package com.bawei.yangzongcai___day2.MaiDongFangHong.day2;


import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bawei.yangzongcai___day2.R;
import com.uuzuche.lib_zxing.activity.CaptureActivity;
import com.uuzuche.lib_zxing.activity.CodeUtils;
@Route(path = "/tiao2/dongfanghong2")
public class MaiDongFangHong2 extends AppCompatActivity {


    private ImageView rv;
    private Button erweima;
    private Button saomiao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mai_dong_fang_hong2);
        initView();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.VIBRATE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 200);
        }

        erweima.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                create();
            }
        });

        saomiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sao();
            }
        });

    }

    //生成二维码
    private void create() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.tubiao_dlam);
        Bitmap code = CodeUtils.createImage("本次纯属娱乐杨浩鑫不会啊啊", 400, 400, bitmap);
        rv.setImageBitmap(code);
    }


    private void initView() {
        rv = findViewById(R.id.rv);
        erweima = findViewById(R.id.erweima);
        saomiao = findViewById(R.id.saomiao);
    }

    //扫描二维码
    private void sao() {
        Intent intent = new Intent(this, CaptureActivity.class);
        startActivityForResult(intent,101);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 101 && resultCode == Activity.RESULT_OK &&data != null){
            Bundle bundle = data.getExtras();//获得扫描的数据
            if(bundle == null){
                return;
            }
            if(bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS){//判断是否成功
                String string = bundle.getString(CodeUtils.RESULT_STRING);//获得二维码字符串
                Toast.makeText(this, "扫描结果"+string, Toast.LENGTH_SHORT).show();
            }
        }
    }

}
